const {
  addrSetDefaultErr,
  addrGetListErr,
  addrUpdateErr,
  addrAddErr
} = require('@constant/error.type');

const { controller, emit } = require('@utils/tools');

const {
  addrAdd,
  addrGetList,
  addrSetDefault,
  addrUpdate,
} = require('./address.service');

module.exports = {
  async add(ctx) {
    const params = {
      user_id: ctx.state.user.id,
      ...ctx.request.body,
    };

    const res = await controller(params, addrAdd);

    emit(ctx, res, res, addrAddErr);
  },
  async update(ctx) {
    const { id, ...values } = ctx.request.body;

    const params = {
      id,
      values,
      user_id: ctx.state.user.id,
    };

    const res = await controller(params, addrUpdate);

    emit(ctx, res, values, addrUpdateErr);
  },
  async setDefault(ctx) {
    const id = ctx.request.body.id;

    const params = { id, user_id: ctx.state.user.id };

    const res = await controller(params, addrSetDefault);

    emit(ctx, res, { id }, addrSetDefaultErr);
  },
  async list(ctx) {
    const params = {
      ...ctx.query,
      user: ctx.state.user,
    };

    const res = await controller(params, addrGetList);

    emit(ctx, res, res, addrGetListErr);
  },
};
